/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 * mmi_gpio_backlight.c
 *
 * Project:
 * --------
 *   MAUI
 *
 * Description:
 * ------------
 *   This file is intends for Utility functions for GPIO
 *
 * Author:
 * -------
 * 
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  *============================================================================
  ****************************************************************************/
 
 /***************************************************************************** 
 * Include
 *****************************************************************************/
#include "MMI_features.h"
#include "kal_non_specific_general_types.h"
#include "custom_equipment.h"
#include "mmiapi_struct.h"

#include "MMIDataType.h"
#include "mmi_frm_timer_gprot.h"
#include "device.h"
#include "TimerEvents.h"
#include "lcd_if.h"
#include "mdi_audio.h"
#include "MMI_common_app_trc.h"
#include "DebugInitDef_Int.h"
#include "kal_trace.h"
#include "wgui_categories_util.h"
#include "gui.h"
#include "stack_config.h"
#include "mmi_frm_queue_gprot.h"
#include "app_ltlcom.h"
#include "stack_msgs.h"
#include "mmi_msg_struct.h"
#include "kal_release.h"

#include "IdleAppGprot.h"
#include "BootupSrvGprot.h"
#include "GpioSrvGprot.h"
#include "GpioSrvprot.h"

#ifdef __MMI_DSM_NEW__
#include "mrp_include.h"
#if defined(__MMI_SKYQQ__) && !defined(WIN32)
#include "sky_qq_open.h"
#endif
#endif


typedef struct
{
    U8 mode;
    U8 is_timer_suspend; /* Is UI timer (aligned) suspended. 0 == non-suspend, 1 == suspend */
    U8 keypad_state;     /* 0 = dark. 1 = light */  
    U8 lcd_state;        /* 0 = dark, 1 = half light, 2 = full light  */
    U8 lcd_level;        /* 0: dark, 5: light */
    U8 lcd_real_level;   /* 0 - 5: from phnset */
    S32 hf_time;
    S32 default_hf_time;
    U8 sublcd_level;
    U8 sublcd_state;     /* 0 = dark, 1 = half light, 2 = full light  */
    S32 sublcd_hf_time; 
    srv_backlight_timer_state_enum timer_status;
} srv_backlight_context_struct;

static srv_backlight_context_struct g_mmi_gpio_context;
static srv_backlight_context_struct *g_gpio_p = &g_mmi_gpio_context;

/***************************************************************************** 
* Local variable
*****************************************************************************/
#if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
static MMI_BOOL is_in_sleep_mode = MMI_FALSE;
#endif


/***************************************************************************** 
* Local function
*****************************************************************************/
static void srv_backlight_lcd_sleep_in(void);
static void srv_backlight_lcd_sleep_out(void);
static void srv_backlight_timer_hdlr(void);


/***************************************************************************** 
* Global function
*****************************************************************************/

/***************************************************************************** 
* External functitons
*****************************************************************************/
extern void UI_BLT_double_buffer(S32 x1, S32 y1, S32 x2, S32 y2);

/*****************************************************************************
 * FUNCTION
 *  srv_backlight_init_context_early
 * DESCRIPTION
 *  set the bl hf time
 * PARAMETERS
 *  action          [IN]        
 *  Action(?)       [IN]        Battery_sttus
 * RETURNS
 *  void
 *****************************************************************************/
void srv_backlight_init_context_early(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    g_gpio_p->hf_time = g_gpio_p->default_hf_time;
    g_gpio_p->mode = 1;
    g_gpio_p->lcd_state = 1;
    g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_resume_time
 * DESCRIPTION
 *  Wake up timer
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
static void srv_backlight_resume_time(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* resume timer if suspended */
    if (g_gpio_p->is_timer_suspend == 1)
    {
        mmi_frm_resume_timers(TIMER_IS_NO_ALIGNMENT);
        g_gpio_p->is_timer_suspend = 0;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mmi_gpio_turn_on_keypad_light
 * DESCRIPTION
 *  turn on keypad backlight
 *  
 *  PARAMETERS:
 *  void
 *****************************************************************************/
static void srv_backlight_turn_on_keypad_light(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* turn on keypad backlight if currently off and clam open */
    if (g_gpio_p->keypad_state == 0 && !srv_clam_is_close() && 
        !g_srv_gpio_property_info.keypad_locked && !mmi_idle_is_keypad_locked())
    {
        srv_backlight_keypad_on();
        g_gpio_p->keypad_state = 1;
        
        StartTimer(GPIO_KEYPAD_LIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_turn_off_keypad_light);
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_off_keypad_light
 * DESCRIPTION
 *  turn off keypad backlight
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_turn_off_keypad_light(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* turn off keypad backlight */
    StopTimer(GPIO_KEYPAD_LIGHT_TIMER);
    if (g_gpio_p->keypad_state == 1)
    {
        srv_backlight_keypad_off();
        g_gpio_p->keypad_state = 0;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_lcd_sleep_in
 * DESCRIPTION
 *  Wake up LCM
 *  PARAMETERS:
 *  void
 *****************************************************************************/
static void srv_backlight_lcd_sleep_in(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
//  MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_srv_backlight_lcd_sleep_in);

#if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
    if (is_in_sleep_mode == MMI_FALSE)
#endif
    {
       /* use emit(not post) to submit, because LCD sleep in should after app sleep in */
        srv_gpio_emit_event(EVT_ID_GPIO_LCD_SLEEP_IN);
        
        lcd_sleep_in(MAIN_LCD);
        
    #if defined(__MMI_SUBLCD__)
        lcd_sleep_in(SUB_LCD);
    #endif
        
#if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
        is_in_sleep_mode = MMI_TRUE;
#endif 
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_lcd_sleep_out
 * DESCRIPTION
 *  Sleep LCM
 *  PARAMETERS:
 *  void
 *****************************************************************************/
static void srv_backlight_lcd_sleep_out(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
//  MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_srv_backlight_lcd_sleep_out);

#if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
    if (is_in_sleep_mode)
#endif 
    {
        lcd_sleep_out(MAIN_LCD);
        
    #if defined(__MMI_SUBLCD__)
        lcd_sleep_out(SUB_LCD);
    #endif
        
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
        is_in_sleep_mode = MMI_FALSE;
    #endif

        srv_gpio_emit_event(EVT_ID_GPIO_LCD_SLEEP_OUT);
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_half_mode
 * DESCRIPTION
 *  Takes the Backlight to Half Mode
 *  (Backlit is dimmed but not black)
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_half_mode(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_gpio_p->mode == 1)   /* for AT command */
    {
        if (g_gpio_p->sublcd_state == 0)
        {
            srv_gpio_set_device_level(GPIO_DEV_LED_SUBLCD, LED_LIGHT_LEVEL5);
            g_gpio_p->sublcd_state = 1;
        }

        if (g_gpio_p->lcd_state == 0 && !srv_clam_is_close())   /* sleep mode */
        {
        #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
            srv_backlight_lcd_sleep_out();
        #endif /* !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) */ 
            srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL1);
            g_gpio_p->lcd_state = 1;
        }

        srv_backlight_resume_time();
        
        if (g_gpio_p->timer_status > SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE)
        {
            StopTimer(BACKLIGHT_TIMER);
            StartTimer(BACKLIGHT_TIMER, LCD_CLAM_OPEN_HALFLITE_TO_OFF_TIME, srv_backlight_timer_hdlr);
            g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_SLEEP_MODE;
        }
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_to_sleep_mode
 * DESCRIPTION
 *  Takes the Backlight to Half Mode
 *  (Backlit is dimmed but not black)
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_to_sleep_mode(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_gpio_p->lcd_state == 1)
    {   
        if (srv_gpio_emit_event(EVT_ID_GPIO_BEFORE_LCD_SLEEP_IN) 
			&& (mdi_audio_get_state() != MDI_AUDIO_RECORD) && (mdi_audio_get_state() != MDI_AUDIO_RECORD_PAUSED)) 
        {
            srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL0);
            g_gpio_p->lcd_state = 0;

            #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) 
                srv_backlight_lcd_sleep_in();
            #endif /* !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) */
        }

        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_IN_SLEEP_MODE;
    }

    if (g_gpio_p->is_timer_suspend == 0  && srv_gpio_emit_event(EVT_ID_GPIO_AFTER_LCD_SLEEP_IN) &&
		mdi_audio_get_state() != MDI_AUDIO_RECORD && mdi_audio_get_state() != MDI_AUDIO_RECORD_PAUSED)
    {
#ifdef __MMI_DSM_NEW__
	if(MR_SUCCESS == mr_app_system_event_hdlr(MR_SYSMSG_SUSPEND_TIMER, 0, 0))
	{
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
		g_srv_backligit_is_sleeping = MMI_FALSE;
#endif
		return;
	}

#endif/*__MMI_DSM_NEW__*/   	

        mmi_frm_suspend_timers(TIMER_IS_NO_ALIGNMENT);
        g_gpio_p->is_timer_suspend = 1;
    }
}

#if defined(__MMI_SYNC_LCD_GPIO_MODE__)


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_on_all_light
 * DESCRIPTION
 *  Turn on all light to whole bright
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_backlight_turn_on_all_light(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_on_all_light(),SYNC_MODE=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURN_ON_ALL_LIGHT, 
                1, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);
    
    srv_backlight_lcd_sleep_out();
    srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
    g_gpio_p->lcd_state = 2;
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_off_all_light
 * DESCRIPTION
 *  Turn off all light to whole dark
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_backlight_turn_off_all_light(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_off_all_light(),,SYNC_MODE=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURN_OFF_ALL_LIGHT,
                1, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);
    
    /* set lcd backlight to half mode */
    if (g_gpio_p->lcd_state != 0) /* backlight is on */
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL0);
        g_gpio_p->lcd_state = 0;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_off
 * DESCRIPTION
 *  It is typically paired with srv_backlight_turn_on(0) in order to resume backlight timer.
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_turn_off(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_off(),SYNC_MODE=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURN_OFF_BACKLIGHT,
                1, g_gpio_p->timer_status, g_gpio_p->lcd_state,  g_gpio_p->sublcd_state);
    
    /* Share the same code for target and PC simulator */

    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)
    {
        StopTimer(BACKLIGHT_TIMER);
        StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_timer_hdlr
 * DESCRIPTION
 *  Timer handler to switch backlight state
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
static void srv_backlight_timer_hdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_timer_hdlr(),SYNC_MODE=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_BACKLIGHT_TIMER_HDLR,
                1, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state); 

    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER) /* resume backlight mechanism */
    {
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
        StopTimer(BACKLIGHT_TIMER);
        StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
    }
    else if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE)
    {
        if (g_gpio_p->lcd_state == 2) /* full light, switch to half light */
        {
            srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL1);
            g_gpio_p->lcd_state = 1;
        }

        StopTimer(BACKLIGHT_TIMER);

        StartTimer(BACKLIGHT_TIMER, LCD_CLAM_OPEN_HALFLITE_TO_OFF_TIME, srv_backlight_timer_hdlr);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_SLEEP_MODE;

        srv_gpio_post_event(EVT_ID_GPIO_BACKLIGHT_DIMMING);         
    }
    else    /* g_gpio_p->timer_status == 2 */
    {
        srv_backlight_to_sleep_mode();
    }
        
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_on
 * DESCRIPTION
 *  Turns On the backlight
 * PARAMETERS
 *  time_enum       [IN]        Time duration type
 *****************************************************************************/
void srv_backlight_turn_on(srv_backlight_timer_type_enum time_enum)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_on(),,SYNC_MODE=%d,timer_enum=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURNON_BACKLIGHT,
                1, time_enum, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);
    
    if (time_enum != SRV_BACKLIGHT_PERMANENT)
    {
        /* turn on keypad backlight if currently off and clam open */
        srv_backlight_turn_on_keypad_light();
    }

    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    srv_backlight_resume_time();

    /* [MMIGPIO]srv_backlight_turn_on(),,lcd_state=%d,clam_IS_close=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURNON_BACKLIGHT_CHECK_CLAM,
                g_gpio_p->lcd_state, srv_clam_is_close());
    /* lcd backlight */
    if (g_gpio_p->lcd_state == 0)
    {
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
        srv_backlight_lcd_sleep_out();
    #endif       
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
        g_gpio_p->lcd_state = 2;
      
    }
    else if (g_gpio_p->lcd_state == 1)
    {    
    #ifndef __MMI_SLIDE__
        if (!srv_clam_is_close() || !srv_bootup_is_completed())   /* clam is opened */
    #endif 
        {
            srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
            g_gpio_p->lcd_state = 2;
        }    
    }
    else if (g_gpio_p->lcd_state == 2 )  /* backlight off */
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
    }
    
    /* start/stop timer according to input parameter */
    if (time_enum == SRV_BACKLIGHT_PERMANENT)
    {
        StopTimer(BACKLIGHT_TIMER);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
    }
    else if (g_gpio_p->timer_status != SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)    /* start a timer */
    {
        StopTimer(BACKLIGHT_TIMER);    
    #if defined (__MMI_CLAMSHELL__) && !defined(__MMI_SLIDE__)
        if (!srv_clam_is_close()) /* clam is opened */
        {
            StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
            g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
        }
        else    /* clam is closed */
        {
            StartTimer(BACKLIGHT_TIMER, LCD_CLAM_CLOSE_HALFLITE_TO_OFF_TIME, srv_backlight_timer_hdlr);
            g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
        }
    #else /* defined (__MMI_CLAMSHELL__) && !defined(__MMI_SLIDE__) */ 
        StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
    #endif /* defined (__MMI_CLAMSHELL__) && !defined(__MMI_SLIDE__) */     
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_close
 * DESCRIPTION
 *  Immeditae Baclight Close not going thru Half Mode
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_close(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_close(),SYNC_MODE=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_CLOSE_BACKLIGHT,
                 1, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);  
    
    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    /* stop backlight timer */
    if (g_gpio_p->timer_status != SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)
    {
        StopTimer(BACKLIGHT_TIMER);
    }

    /* set lcd backlight to half mode */
    if (g_gpio_p->lcd_state == 2) /* backlight is on */
    {
        g_gpio_p->lcd_state = 1;
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL1);
    }
    else if (g_gpio_p->lcd_state == 0)    /* backlight is off */
    {
        g_gpio_p->lcd_state = 1;
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
        srv_backlight_lcd_sleep_out();
        update_mainlcd_dt_display();
        update_sublcd_dt_display();
        gdi_layer_blt_previous(0, 0, UI_device_width - 1, UI_device_height - 1);

        srv_backlight_resume_time();
        is_in_sleep_mode = MMI_FALSE;
        
        srv_gpio_set_device_level(GPIO_DEV_LED_SUBLCD, LED_LIGHT_LEVEL5);
    #endif /* !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) */ 
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
    }

    /* turn off keypad backlight */
    srv_backlight_turn_off_keypad_light();
    StartTimer(BACKLIGHT_TIMER, LCD_CLAM_CLOSE_HALFLITE_TO_OFF_TIME, srv_backlight_timer_hdlr);
    g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_SLEEP_MODE;
}

#else /* defined(__MMI_SYNC_LCD_GPIO_MODE__) */ 


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_on_all_light
 * DESCRIPTION
 *  Turn on all light to whole bright
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_backlight_turn_on_all_light(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_on_all_light(),SYNC_MODE=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURN_ON_ALL_LIGHT, 
                0, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);
    
    srv_backlight_lcd_sleep_out();
    srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
    g_gpio_p->lcd_state = 2;

    srv_gpio_set_device_level(GPIO_DEV_LED_SUBLCD, LED_LIGHT_LEVEL5);
    g_gpio_p->sublcd_state = 2;
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_off_all_light
 * DESCRIPTION
 *  Turn off all light to whole dark
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
static void srv_backlight_turn_off_all_light(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_off_all_light(),,SYNC_MODE=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURN_OFF_ALL_LIGHT,
                0, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);
    
    /* set lcd backlight to half mode */
    if (g_gpio_p->lcd_state != 0) /* backlight is on */
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL0);
        g_gpio_p->lcd_state = 0;
    }

    /* turn on sub-lcd baclight */
    if (g_gpio_p->sublcd_state != 0)
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_SUBLCD, LED_LIGHT_LEVEL0);
        g_gpio_p->sublcd_state = 0;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_off
 * DESCRIPTION
 *  Turns Off the backlight
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_turn_off(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_off(),SYNC_MODE=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURN_OFF_BACKLIGHT,
                0, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);
        
    /* Share the same code for target and PC simulator */

    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER) /* resume backlight mechanism */
    {
        StopTimer(BACKLIGHT_TIMER);
        StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_timer_hdlr
 * DESCRIPTION
 *  Timer handler to switch backlight state
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
static void srv_backlight_timer_hdlr(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_timer_hdlr(),SYNC_MODE=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_BACKLIGHT_TIMER_HDLR,
                0, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);    
    
    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER) /* resume backlight mechanism */
    {
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
        StopTimer(BACKLIGHT_TIMER);
        StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
    }
    else if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE)
    {
        if (g_gpio_p->lcd_state == 2) /* full light, switch to half light */
        {
            srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL1);
            g_gpio_p->lcd_state = 1;
        }
        
        StopTimer(BACKLIGHT_TIMER);

    #ifdef __MMI_CLAMSHELL__
        if (srv_clam_is_close())
        {
            /* [MMIGPIO]srv_backlight_timer_hdlr,clam_is_close,StartTimer" */
            MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_CLAM_IS_CLOSE_START_TIME);
            StartTimer(BACKLIGHT_TIMER, LCD_CLAM_CLOSE_HALFLITE_TO_OFF_TIME, srv_backlight_timer_hdlr);
        }
        else
    #endif /* __MMI_CLAMSHELL__ */ 
            StartTimer(BACKLIGHT_TIMER, LCD_CLAM_OPEN_HALFLITE_TO_OFF_TIME, srv_backlight_timer_hdlr);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_SLEEP_MODE;

        srv_gpio_emit_event(EVT_ID_GPIO_BACKLIGHT_DIMMING);
        
    }
    else    /* g_gpio_p->timer_status == 2 */
    {
        srv_backlight_to_sleep_mode();
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_turn_on
 * DESCRIPTION
 *  Turns On the backlight
 * PARAMETERS
 *  time_enum       [IN]        Time duration type
 *****************************************************************************/
void srv_backlight_turn_on(srv_backlight_timer_type_enum time_enum)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_turn_on(),,SYNC_MODE=%d,timer_enum=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURNON_BACKLIGHT,
                0, time_enum, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);    
    
    if (time_enum != SRV_BACKLIGHT_PERMANENT)
    {
        /* turn on keypad backlight if currently off and clam open */
        srv_backlight_turn_on_keypad_light();
    }

    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    srv_backlight_resume_time();

    /* turn on sub-lcd backlight if currently off */
    if (g_gpio_p->sublcd_state == 0)
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_SUBLCD, LED_LIGHT_LEVEL5);
        g_gpio_p->sublcd_state = 1;
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) && defined(__MMI_SUBLCD__)
        {
            S32 lcd_width;
            S32 lcd_height;

            gdi_lcd_set_active(GDI_LCD_SUB_LCD_HANDLE);
            gdi_lcd_get_dimension(&lcd_width, &lcd_height);
            UI_BLT_double_buffer(0, 0, lcd_width - 1, lcd_height - 1);
            gdi_lcd_set_active(GDI_LCD_MAIN_LCD_HANDLE);

        }
    #endif /* !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) && defined(__MMI_SUBLCD__) */ 
    }
    /* [MMIGPIO]srv_backlight_turn_on(),,lcd_state=%d,clam_IS_close=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_TURNON_BACKLIGHT_CHECK_CLAM,
                g_gpio_p->lcd_state, srv_clam_is_close());
    if (g_gpio_p->lcd_state == 0 && !srv_clam_is_close())   /* sleep mode and clam open */
    {
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)

        srv_backlight_lcd_sleep_out();
	
    	update_mainlcd_dt_display();
		update_sublcd_dt_display();
		UI_BLT_double_buffer(0, 0, UI_device_width - 1, UI_device_height - 1);
    #endif /* !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) */ 

        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
    
        g_gpio_p->lcd_state = 2;
    }
    else if (g_gpio_p->lcd_state == 1 && !srv_clam_is_close())  /* backlight off and clam open */
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
        g_gpio_p->lcd_state = 2;
    }
    else if (g_gpio_p->lcd_state == 2 && !srv_clam_is_close())  /* backlight off and clam open */
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL5);
    }
   
    /* start/stop timer according to input parameter */
    if (time_enum == SRV_BACKLIGHT_PERMANENT)
    {
        StopTimer(BACKLIGHT_TIMER);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
    }
    else if (g_gpio_p->timer_status != SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)    /* start a timer */
    {
        StopTimer(BACKLIGHT_TIMER);
        StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
        g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_HALF_MODE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_close
 * DESCRIPTION
 *  Immeditae Baclight Close not going thru Half Mode
 *  We do not handle __MMI_LCD_PARTIAL_ON__ because srv_backlight_close() is used only in clamshell
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_close()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* [MMIGPIO]srv_backlight_close(),SYNC_MODE=%d,timer_status=%d,lcd_state=%d,sublcd_state=%d */
    MMI_TRACE(MMI_COMMON_TRC_G8_DEV, TRC_MMI_GPIO_CLOSE_BACKLIGHT,
                0, g_gpio_p->timer_status, g_gpio_p->lcd_state, g_gpio_p->sublcd_state);   
    
    /* AT command disables backlight mechanism */
    if (g_gpio_p->mode == 0)
    {
        return;
    }

    /* stop backlight timer */
    if (g_gpio_p->timer_status != SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)
    {
        StopTimer(BACKLIGHT_TIMER);
    }

    /* turn off main-lcd backlight */
    if (g_gpio_p->lcd_state != 0)
    {
        srv_gpio_set_device_level(GPIO_DEV_LED_MAINLCD, LED_LIGHT_LEVEL0);
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
        srv_backlight_lcd_sleep_in();

    #endif /* !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__) */ 
        g_gpio_p->lcd_state = 0;
    }

    /* turn off keypad backlight */
    srv_backlight_turn_off_keypad_light();

#ifdef __MMI_SUBLCD__
    /* turn on sub-lcd baclight */
    if (g_gpio_p->sublcd_state == 0)
    {
        srv_backlight_resume_time();
    #if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
        is_in_sleep_mode = MMI_FALSE;
    #endif 
        srv_backlight_lcd_sleep_out();
        srv_gpio_set_device_level(GPIO_DEV_LED_SUBLCD, LED_LIGHT_LEVEL5);
        g_gpio_p->sublcd_state = 1;
    }
#endif

    StartTimer(BACKLIGHT_TIMER, g_gpio_p->hf_time * 1000, srv_backlight_timer_hdlr);
    g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_FOR_SLEEP_MODE;
}

#endif /* defined(__MMI_SYNC_LCD_GPIO_MODE__) */ 


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_ctrl_req_hdlr
 * DESCRIPTION
 *  Handler of MSG_ID_MMIAPI_BACKLIGHT_CONTROL_REQ
 * PARAMETERS
 *  msg     [IN]        Requested data
 * RETURNS
 *  void
 *****************************************************************************/
void srv_backlight_ctrl_req_hdlr(void *msg, int mod_id , void *peer_buf)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmiapi_backlight_control_req_struct *req = (mmiapi_backlight_control_req_struct*) msg;
    mmiapi_backlight_control_rsp_struct *rsp;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    StopTimer(BACKLIGHT_TIMER);

    if (req->on_off)
    {
        /* solve MED issue, who can't resume timer */
        srv_backlight_resume_time();
        
        if (req->disable_timer)
        {
            srv_backlight_turn_on_all_light();
            g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
            g_gpio_p->mode = 0;
        }
        else
        {
            if (g_gpio_p->timer_status < SRV_BACKLIGHT_TIMER_STATE_FOR_SLEEP_MODE || g_gpio_p->mode == 0)
            {   
                /* if the timer is disable, resume timer */
                if (g_gpio_p->mode == 0)
                {
                    g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
                    g_gpio_p->mode = 1;
                    srv_backlight_turn_off();
                }

                /* resume backlight timer and turn on the backlight */
                srv_backlight_turn_on(SRV_BACKLIGHT_SHORT_TIME);
            }
            else
            {
                g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
                g_gpio_p->mode = 1;
                srv_backlight_turn_off();
            }
        }
    }
    else
    {
        /* solve audio synchronize stupid strange issue. */
        if((mod_id == MOD_L1SP || mod_id == MOD_MED) && (!mdi_audio_is_idle()))
        {
            srv_backlight_turn_off_all_light();
        }

        if (req->disable_timer)
        {
            StopTimer(BACKLIGHT_TIMER);
            g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
            g_gpio_p->mode = 0;
        }
        else
        {
            g_gpio_p->timer_status = SRV_BACKLIGHT_TIMER_STATE_NO_TIMER;
            g_gpio_p->mode = 1;
            srv_backlight_turn_off();
        }      
    }

    rsp = (mmiapi_backlight_control_rsp_struct*) 
        OslConstructDataPtr(sizeof(mmiapi_backlight_control_rsp_struct));   

    if (g_gpio_p->timer_status == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER) /* timer disabled */
    {
        rsp->disable_timer = TRUE;
    }
    else
    {
        rsp->disable_timer = FALSE;
    }

    rsp->on_off = req->on_off;

    if (mod_id == MOD_L1SP)
    {
        srv_gpio_send_message(MOD_MED, MSG_ID_MMIAPI_BACKLIGHT_CONTROL_RSP, rsp, NULL);
    }
    else
    {
        srv_gpio_send_message(mod_id, MSG_ID_MMIAPI_BACKLIGHT_CONTROL_RSP, rsp, NULL);
    }   
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_lcd_keypad_off
 * DESCRIPTION
 *  srv_backlight_lcd_keypad_off
 *  
 *  PARAMETERS: void
 *  void
 *  RETURNS:
 *  void
 *****************************************************************************/
void srv_backlight_lcd_keypad_off(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    StopTimer(TIMER_KEYPAD_BACKLIGHT);
    srv_backlight_all_lcd_off();
    srv_backlight_keypad_off();
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_set_mode_exe
 * DESCRIPTION
 *  Sets BackLight Mode
 *  
 *  PARAMETERS: void
 *  mode        [IN]        
 *****************************************************************************/
void srv_backlight_set_mode_exe(U8 mode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mode == 1)  /* enable backlight mechanism. */
    {
        g_gpio_p->mode = mode;
        srv_backlight_turn_off();
    }
    else    /* disable backlight mechanism. */
    {
        g_gpio_p->mode = 1;
        srv_backlight_turn_on(SRV_BACKLIGHT_PERMANENT);
        g_gpio_p->mode = mode;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_set_mode
 * DESCRIPTION
 *  Protocol Handler for MSG_ID_MMI_EQ_SET_SLEEP_MODE_REQ_IND
 *  
 *  PARAMETERS: info: Data from L4
 *  info        [IN]        
 *****************************************************************************/
void srv_backlight_set_mode(void *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_eq_set_sleep_mode_req_ind_struct *msg = (mmi_eq_set_sleep_mode_req_ind_struct*) info;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    srv_backlight_set_mode_exe((U8) msg->on_off);
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_set_hf_time
 * DESCRIPTION
 *  set the bl hf time
 * PARAMETERS
 *  action          [IN]        
 *  Action(?)       [IN]        Battery_sttus
 * RETURNS
 *  void
 *****************************************************************************/
void srv_backlight_set_hf_time(srv_backlight_set_timer_type_enum type, S32 time)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (type == SRV_BACKLIGHT_SET_TIMER_DEFAULT)
    {
        g_gpio_p->hf_time =  g_gpio_p->default_hf_time = time;        
    }
    else if (type == SRV_BACKLIGHT_SET_TIMER_BY_APP)
    {
        g_gpio_p->hf_time = time;
    }
    else
    {
        ASSERT(0);
    }    
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_get_hf_time
 * DESCRIPTION
 *  get the bl hf time
 * PARAMETERS
 * RETURNS
 *  void
 *****************************************************************************/
S32 srv_backlight_get_hf_time(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return g_gpio_p->hf_time;
}

/*****************************************************************************
 * FUNCTION
 *  mmi_gpio_is_default_backlight_hf_time
 * DESCRIPTION
 *  get the bl hf time
 * PARAMETERS
 * RETURNS
 *  if hf timer equal with setting, return MMI_TRUE, otherwise return MMI_FALSE.
 *****************************************************************************/
MMI_BOOL srv_backlight_is_default_hf_time(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_gpio_p->hf_time == g_gpio_p->default_hf_time)
        return MMI_TRUE;
    else
        return MMI_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  mmi_gpio_get_backlight_level
 * DESCRIPTION
 *  Gets current backlight level
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
U8 srv_backlight_get_level(srv_backlight_device_type_enum lcd_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (lcd_type == SRV_BACKLIGHT_TYPE_MAINLCD)
    {
        return g_gpio_p->lcd_level;
    }
    else if (lcd_type == SRV_BACKLIGHT_TYPE_SUBLCD)
    {
        return g_gpio_p->sublcd_level;
    }
    else if (lcd_type == SRV_BACKLIGHT_TYPE_KEYPAD)
    {
        return g_gpio_p->keypad_state;
    }
    else
    {
        ASSERT(0);

        return 0;
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_update_level
 * DESCRIPTION
 *  sets current backlight level
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
void srv_backlight_update_level(srv_backlight_device_type_enum lcd_type, U8 level)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (lcd_type == SRV_BACKLIGHT_TYPE_MAINLCD)
    {
        g_gpio_p->lcd_real_level = level;
    }
    else if (lcd_type == SRV_BACKLIGHT_TYPE_SUBLCD)
    {
        g_gpio_p->sublcd_level = level;
    }
    else
    {
    }
}

/*****************************************************************************
 * FUNCTION
 *  srv_backlight_set_real_level
 * DESCRIPTION
 *  sets current backlight level 
 *  PARAMETERS: 
 *  lcd_type    [IN]        lcd type
 *  level       [IN]        backlight level
 * RETURNS
 *  void
   *****************************************************************************/
void srv_backlight_set_real_level(srv_backlight_device_type_enum lcd_type, U8 level)

{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (lcd_type == SRV_BACKLIGHT_TYPE_MAINLCD)
    {
        g_gpio_p->lcd_real_level = level;
    }
    else if (lcd_type == SRV_BACKLIGHT_TYPE_SUBLCD)
    {
        g_gpio_p->sublcd_level = level;
    }
    else
    {
    }
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_get_level_from_setting
 * DESCRIPTION
 *  get current backlight level  
 *  PARAMETERS: 
 *  lcd_type    [IN]        lcd type
 *  RETURNS
 *  real backlight level.
 *****************************************************************************/
U8 srv_backlight_get_real_level(srv_backlight_device_type_enum lcd_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (lcd_type == SRV_BACKLIGHT_TYPE_MAINLCD)
    {
        return g_gpio_p->lcd_real_level;
    }
    else if (lcd_type == SRV_BACKLIGHT_TYPE_SUBLCD)
    {
        return g_gpio_p->sublcd_level;
    }
    else
    {
    }
    return 0;
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_get_timer_state
 * DESCRIPTION
 *  get backlight timer state
 *  
 *  PARAMETERS:
 *  void
 *****************************************************************************/
srv_backlight_timer_state_enum srv_backlight_get_timer_state(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return g_gpio_p->timer_status;
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_is_on
 * DESCRIPTION
 *  Gets current backlight level
 *  
 *  PARAMETERS: void
 *  void
 *****************************************************************************/
MMI_BOOL srv_backlight_is_on(srv_backlight_device_type_enum lcd_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL ret=MMI_FALSE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch(lcd_type)
    {
        case SRV_BACKLIGHT_TYPE_MAINLCD:
        {
            if (g_gpio_p->lcd_state != 0)
            {
                ret = MMI_TRUE;
            }
            break;
        }
        case SRV_BACKLIGHT_TYPE_SUBLCD:
        {
            if (g_gpio_p->sublcd_state != 0)
            {
                ret = MMI_TRUE;
            }
            break;
        }
        default:
            break;
    }

    return ret;
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_is_lcd_sleep
 * DESCRIPTION
 *  To tell applications that lcd is sleeping or not
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL srv_backlight_is_lcd_sleep(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if !defined(__MMI_GPIO_DISABLE_SLEEP_MODE__)
    return is_in_sleep_mode;
#else
    return MMI_FALSE;
#endif
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_at_set_level_hdlr
 * DESCRIPTION
 *  AT command set backlight level
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void srv_backlight_at_set_level_hdlr(void *msg, int source, void *data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_at_general_res_req_struct *rsp;
    mmi_eq_exe_gpio_level_req_ind_struct *req;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    req = (mmi_eq_exe_gpio_level_req_ind_struct*) msg;
    rsp = (mmi_at_general_res_req_struct*) OslConstructDataPtr(sizeof(mmi_at_general_res_req_struct));
    switch(req->gpio_dev_type)
    {
        case GPIO_DEV_LED_MAINLCD:
        {
            rsp->result = KAL_TRUE;
            
            if (req->gpio_dev_level == 0)
            {
                srv_backlight_close();
                srv_backlight_turn_off();
            }
            else if (req->gpio_dev_level == 5)
            {
                if (req->duration == 0xFFFF)
                {
                    srv_backlight_turn_on(SRV_BACKLIGHT_PERMANENT);
                }
                else if (srv_backlight_get_timer_state() == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)
                {
                    /* backlight turn on permanent */
                    rsp->result = KAL_FALSE;
                }
                else
                {
                    if (req->duration == 0)
                    {
                        g_gpio_p->hf_time =  g_gpio_p->default_hf_time;
                        
                        srv_backlight_turn_on(SRV_BACKLIGHT_SHORT_TIME);
                    }                
                    else
                    {
                        g_gpio_p->hf_time = (S32)req->duration;
                        srv_backlight_turn_on(SRV_BACKLIGHT_SHORT_TIME);
                    }
                }
            }
            else
            {
                rsp->result = KAL_FALSE;
            }
            
            break;
        }

        default:
            rsp->result = KAL_FALSE;
            break;
    }

    srv_gpio_send_message(source, MSG_ID_MMI_AT_GENERAL_RES_REQ, (void*)rsp, NULL);
}


/*****************************************************************************
 * FUNCTION
 *  srv_backlight_at_query_level_hdlr
 * DESCRIPTION
 *  AT command set backlight level
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void srv_backlight_at_query_level_hdlr(void *msg, int source, void *data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    mmi_at_cbklt_query_res_req_struct *rsp;
    MMI_BOOL backlight_on;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/    
    rsp = (mmi_at_cbklt_query_res_req_struct*) OslConstructDataPtr(sizeof(mmi_at_cbklt_query_res_req_struct));
   
    backlight_on = srv_backlight_is_on(SRV_BACKLIGHT_TYPE_MAINLCD);
    
    if (backlight_on)
        rsp->level = 5;
    else
        rsp->level = 0;

    if (srv_backlight_get_timer_state() == SRV_BACKLIGHT_TIMER_STATE_NO_TIMER)
    {
        rsp->duration = 0xFFFF;
    }
    else if (g_gpio_p->hf_time ==  g_gpio_p->default_hf_time)
    {
        rsp->duration = 0;
    }
    else
    {
        rsp->duration = g_gpio_p->hf_time;
    }

    srv_gpio_send_message(source, MSG_ID_MMI_AT_CBKLT_QUERY_RES_REQ, (void*)rsp, NULL);
}

